package uk.ac.ebi.literature.utils.string;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

public class StringUtils {
    private static final Logger logger = Logger.getLogger(StringUtils.class.getName());

    /**
     * @param oldUrl
     * @return
     */
    public static String removeAuthentificationCode(String oldUrl) {
        logger.info("Old URL with authentification code: " + oldUrl);
        String patternStr = "code=\\w*";
        String replacementStr = "";

        // Compile regular expression
        Pattern pattern = Pattern.compile(patternStr);

        // Replace all occurrences of pattern in oldUrl
        Matcher matcher = pattern.matcher(oldUrl);
        String newUrl = matcher.replaceAll(replacementStr);

        logger.info("New URL without authentification code: " + newUrl);
        return newUrl;
    }

    public static boolean hasContent(String str) {
        if (str == null) {
            return false;
        }

        str = str.trim();

        if (str == null || str.isEmpty()) {
            return false;
        }

        if (str.equalsIgnoreCase("null")) {
            return false;
        }

        return true;
    }
}
